/*
 * @Author: cailinxi
 * @Date: 2024-05-08 14:52:21
 * @LastEditTime: 2024-07-04 11:28:26
 * @Description: 游戏主架构，进入热更的游戏界面时初始化的，管理所有重要model或着system，通过该脚本初始化其他游戏物体
 */

using System.Threading;
using Cysharp.Threading.Tasks;
using QFramework;
using Sirenix.OdinInspector;
using Unity.VisualScripting;
using UnityEngine;

public class MainArchitecture : MonoBehaviour
{
    private bool m_Init = false;
    public static IArchitecture Interface
    {
        get;
        private set;
    }
    public static MainArchitecture Instance;
    public Transform UIRoot;
    private async UniTaskVoid Awake()
    {
        if (m_Init) return;
        Instance = this;
        Interface = MainFramework.Interface;
        DontDestroyOnLoad(gameObject);
        DontDestroyOnLoad(UIRoot);
        m_Init = true;
        Interface.GetSystem<UIManager>().UIRoot = UIRoot;
        Interface.GetSystem<UIManager>().OpenUIPanel("UIPanel/UIStartGame/UIStartGame").Forget();
    }

    public async UniTaskVoid LoadGameStart()
    {
        Debug.Log("MainArchitecture.LoadGameStart");
        var handle = await Resources.LoadAsync("UIPanel/UIStartGame/UIStartGame");
        var go = handle.GameObject();
        Instantiate(go, UIRoot);
    }


    private void Update() 
    {
        if(Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    public void OnDestroy()
    {
        Debug.Log("MainArchitecture.OnDestroy");
        Interface.Deinit();
    }
}

public class MainFramework : Architecture<MainFramework>
{
    protected override void Init()
    {
        RegisterSystem(new UIManager());
        RegisterSystem<ProgressSystem>(new ProgressSystem());
        RegisterModel(new StageModel());
        RegisterSystem(new AudioSystem());
    }
}